package com.arcbbs.service.impl;

import com.arcbbs.entity.*;
import com.arcbbs.exception.BaseException;
import com.arcbbs.mapper.FormArticleMapper;
import com.arcbbs.mapper.ForumCommentMapper;
import com.arcbbs.mapper.LikeRecordMapper;
import com.arcbbs.mapper.UserMessageMapper;
import com.arcbbs.service.LikeRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class LikeRecordServiceImpl implements LikeRecordService {
    @Resource
    private UserMessageMapper userMessageMapper;
    @Resource
    private LikeRecordMapper likeRecordMapper;
    @Resource
    private FormArticleMapper formArticleMapper;

    @Override
    public void dolike(String articleId, String userId, String nickName, int opType) {
        UserMessage userMessage = new UserMessage();
        userMessage.setCreateTime(new Date());
        LikeRecord likeRecord = null;
        switch (opType) {
            case 0:
              likeRecord= articlelike(articleId,userId,opType);
              ForumArticleVo forumArticle=formArticleMapper.getbyid(articleId);
              userMessage.setArticleId(forumArticle.getArticleId());
              userMessage.setArticleTitle(forumArticle.getTitle());
              userMessage.setMessageType(2);
              userMessage.setCommentId(0);
              userMessage.setReceivedUserId(forumArticle.getUserId());
                break;
            case 1:
                ForumComment forumComment = forumCommentMapper.getbyid(Integer.valueOf(articleId));

                commentLike(articleId,userId,opType);
                ForumArticleVo forumArticle1=formArticleMapper.getbyid(forumComment.getArticleId());
                userMessage.setArticleId(articleId);
                userMessage.setArticleTitle(forumArticle1.getTitle());
                userMessage.setMessageType(3);
                userMessage.setCommentId(forumComment.getCommentId());
                userMessage.setReceivedUserId(forumArticle1.getUserId());
                break;
        }
        userMessage.setSendUserId(userId);
        userMessage.setSendNickName(nickName);
        userMessage.setStatus(1);
        if (likeRecord == null && !userId.equals(userMessage.getReceivedUserId())) {
            userMessageMapper.insert(userMessage);
        }
    }

    public LikeRecord articlelike(String objId, String userId,int opType) {
        LikeRecord likeRecord = likeRecordMapper.getbyobjectiduseridoptype(objId, userId, opType);
        if (likeRecord != null) {
            likeRecordMapper.deletebyobjanduserid(objId, userId, opType);
            formArticleMapper.updateArticleNumber(1, -1, objId);
        } else {
            ForumArticleVo forumArticle = formArticleMapper.getbyid(objId);
            if (null == forumArticle) {
                throw new BaseException("文章不存在");
            }
            LikeRecord likeRecord1 = new LikeRecord();
            likeRecord1.setObjectId(objId);
            likeRecord1.setUserId(userId);
            likeRecord1.setOpType(0);
            likeRecord1.setCreateTime(new Date());
            likeRecord1.setAuthorUserId(forumArticle.getUserId());
            likeRecordMapper.insert(likeRecord1);
            formArticleMapper.updateArticleNumber(1, 1, objId);
        }
        return likeRecord;
    }
    @Resource
    private ForumCommentMapper forumCommentMapper;
    public LikeRecord commentLike(String objId,String userId,int opType){
        LikeRecord likeRecord = likeRecordMapper.getbyobjectiduseridoptype(objId, userId, opType);
        if (likeRecord != null) {
            likeRecordMapper.deletebyobjanduserid(objId, userId, opType);
            forumCommentMapper.updategoodcount(-1, objId);
        } else {
            ForumComment forumComment = forumCommentMapper.getbyid(Integer.valueOf(objId));
            if (null == forumComment) {
                throw new BaseException("评论不存在");
            }
            LikeRecord likeRecord1 = new LikeRecord();
            likeRecord1.setObjectId(objId);
            likeRecord1.setUserId(userId);
            likeRecord1.setOpType(1);
            likeRecord1.setCreateTime(new Date());
            likeRecord1.setAuthorUserId(forumComment.getUserId());
            likeRecordMapper.insert(likeRecord1);
            forumCommentMapper.updategoodcount( 1, objId);
        }
        return likeRecord;
    }
}
